package com.opo5.logisticsmanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.domain.Depot;
import com.opo5.logisticsmanagement.domain.Material;
import com.opo5.logisticsmanagement.service.LocatorService;
import com.opo5.logisticsmanagement.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/locators")
@CrossOrigin
public class LocatorController {

    @Autowired
    private LocatorService locatorService;

    @Autowired
    private MaterialService materialService;

    /**
     * 根据仓库名称、库位名称、库位容积进行查询
     */
    @GetMapping
    public R getAll(String depotName,String locatorName,Integer locatorCapacity){
        Depot depot = locatorService.getAll(depotName,locatorName,locatorCapacity);
        return R.success(depot);
    }


    /**
     * 根据库位id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteByLocatorId(@PathVariable Integer id){
        log.info("删除库位信息");
        LambdaQueryWrapper<Material> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Material::getLocatorId,id);
        List<Material> list = materialService.list(lqw);
        if(CollectionUtils.isEmpty(list)){
            locatorService.removeById(id);
            return R.success("删除成功");
        }
        return R.success("库位里还有东西，不要删我~~");
    }
}
